﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Windows;

namespace SilverTile
{
    public class Frame : IFrame
    {

        public Frame(int width, int height)
        {
            Width = width;
            Height = height;
            Pixels = new int[width * height];
        }

        public Frame(Uri imageSource)
        {

            BitmapImage img = new BitmapImage();
            img.SetSource(Application.GetResourceStream(imageSource).Stream);
            WriteableBitmap wb = new WriteableBitmap(img);

            Width = wb.PixelWidth;
            Height = wb.PixelHeight;
            Pixels = new int[Width * Height];
            Array.Copy(wb.Pixels, Pixels, wb.Pixels.Length);

        }

        public Frame(string imageSource)
            : this(new Uri(imageSource, UriKind.Relative))
        { }

        #region IFrame Members

        public int Width
        {
            get;
            private set;
        }

        public int Height
        {
            get;
            private set;
        }

        public int[] Pixels
        {
            get;
            private set;
        }

        public int OriginX { get; set; }
        public int OriginY { get; set; }

        private Rect _bounds = Rect.Empty;
        public Rect Bounds
        {
            get {
                if (_bounds.EmptyRect())
                {
                    _bounds = new Rect(0, 0, Width, Height);
                }
                return _bounds;
            }
            set
            {
                _bounds = value;
                if (_backgroundBounds.EmptyRect())
                {
                    _backgroundBounds = _bounds;
                }
            }
        }

        private Rect _backgroundBounds = Rect.Empty;
        public Rect BackgroundBounds
        {
            get
            {
                if (_backgroundBounds.EmptyRect())
                {
                    return Bounds;
                }
                return _backgroundBounds;
            }
            set { _backgroundBounds = value; }
        }

        #endregion


    }
}
